package appTool

import (
	"fmt"
	"github.com/spf13/cobra"
)

var BuildTime string //编译时间
var GoVersion string //go版本
var GitBranch string //代码分支
var CommitId string  //提交版本号
var Version string   //应用版本信息

type appVer struct {
	cmd           *cobra.Command
	version       string
	showBuildTime *bool
}

func newAppVerCommand() *appVer {
	ret := &appVer{}
	ret.cmd = &cobra.Command{
		Use:   "version",
		Short: "版本",
		Long:  `系统版本`,
		Run:   ret.versionCmdRun,
	}
	ret.showBuildTime = ret.cmd.Flags().BoolP("all", "a", false, "编译环境")
	return ret
}

func (a *appVer) versionCmdRun(cmd *cobra.Command, args []string) {
	if a.version == "" {
		a.version = Version
	}
	if *a.showBuildTime {
		fmt.Printf("版本: %s\n编译时间: %s\nGo版本: %s\n", a.version, BuildTime, GoVersion)
		if GitBranch != "" {
			if CommitId != "" {
				fmt.Println("代码: " + GitBranch + "[" + CommitId + "]")
			} else {
				fmt.Println("代码: " + GitBranch)
			}
		}
	} else {
		fmt.Println(a.version)
	}
}
